package org.sdnstproject.interlayer.OF13Protocal;

import java.util.HashMap;

import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuRoleRequest {
	private static final Logger log = LoggerFactory.getLogger(FuRoleRequest.class);
    public byte version = (byte)4;
    public byte type = (byte)24;
    public short length = (short)24;
    public long xid = 0x0L;//需要dummyswitch设置
    public int role = 0x2;
    public long generationId = 0L;
    
    /*public final static int ROLE_NOCHANGE_VAL = 0x0;
    public final static int ROLE_EQUAL_VAL = 0x1;
    public final static int ROLE_MASTER_VAL = 0x2;
    public final static int ROLE_SLAVE_VAL = 0x3;
     * */
    public FuRoleRequest() {
    	
    }
    public FuRoleRequest setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "role":
			this.role = Integer.parseInt(value);
			break;
		case "generationId":
			this.generationId = Long.parseLong(value);
			break;
		}
		return this;
	}
    
    public HashMap<String, String> FuRoleRequestFields = new HashMap<String, String>() {
		{
			put("version","byte");
			put("type","byte");
			put("length","short");
			put("xid","long");
			put("role", "int");
			put("generationId", "long");
		}
    };
    
    public ByteBuf writeTo() {
    	ByteBuf bb = Unpooled.directBuffer();
        // fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 24
        bb.writeByte(type);
        // fixed value property length = 24
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        bb.writeInt(role);
        // pad: 4 bytes
        bb.writeZero(4);
        bb.writeLong(generationId);
        return bb;
    }
    
    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFRoleRequestVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(", ");
        b.append("role=").append(role);
        b.append(", ");
        b.append("generationId=").append(generationId);
        b.append(")");
        return b.toString();
    }
}
